#!/bin/sh
echo "sed elastalert ......"
sed -i "s/RULE_NAME/${RULE_NAME}/g" /opt/elastalert/rules/rule.yaml
sed -i "s/RULE_INDEX/${RULE_INDEX}/g" /opt/elastalert/rules/rule.yaml
sed -i "s/RULE_FILTER_LEVEL/${RULE_FILTER_LEVEL}/g" /opt/elastalert/rules/rule.yaml
sed -i "s/RULE_TYPE/${RULE_TYPE}/g" /opt/elastalert/rules/rule.yaml
sed -i "s/RULE_NUM_EVENTS/${RULE_NUM_EVENTS}/g" /opt/elastalert/rules/rule.yaml
sed -i "s/RULE_TIMEFRAME_HOURS/${RULE_TIMEFRAME_HOURS}/g" /opt/elastalert/rules/rule.yaml
sed -i "s/RULE_ALERT/${RULE_ALERT}/g" /opt/elastalert/rules/rule.yaml
sed -i "s/RULE_FROM_ADDR/${RULE_FROM_ADDR}/g" /opt/elastalert/rules/rule.yaml
sed -i "s/RULE_EMAIL/${RULE_EMAIL}/g" /opt/elastalert/rules/rule.yaml
sed -i "s/RULE_DINGTALK_WEBHOOK/${RULE_DINGTALK_WEBHOOK}/g" /opt/elastalert/rules/rule.yaml
sed -i "s/RULE_DINGTALK_MSGTYPE/${RULE_DINGTALK_MSGTYPE}/g" /opt/elastalert/rules/rule.yaml

sed -i "s/RULE_SPARE_ERROR_NAME/${RULE_SPARE_ERROR_NAME}/g" /opt/elastalert/rules/rule_spare_error.yaml
sed -i "s/RULE_SPARE_ERROR_INDEX/${RULE_SPARE_ERROR_INDEX}/g" /opt/elastalert/rules/rule_spare_error.yaml
sed -i "s/RULE_FILTER_APP/${RULE_FILTER_APP}/g" /opt/elastalert/rules/rule_spare_error.yaml
sed -i "s/RULE_SPARE_ERROR_TYPE/${RULE_SPARE_ERROR_TYPE}/g" /opt/elastalert/rules/rule_spare_error.yaml
sed -i "s/RULE_SPARE_ERROR_NUM_EVENTS/${RULE_SPARE_ERROR_NUM_EVENTS}/g" /opt/elastalert/rules/rule_spare_error.yaml
sed -i "s/RULE_SPARE_ERROR_TIMEFRAME_MINUTES/${RULE_SPARE_ERROR_TIMEFRAME_MINUTES}/g" /opt/elastalert/rules/rule_spare_error.yaml
sed -i "s/RULE_SPARE_ERROR_ALERT/${RULE_SPARE_ERROR_ALERT}/g" /opt/elastalert/rules/rule_spare_error.yaml
sed -i "s/RULE_SPARE_ERROR_DINGTALK_WEBHOOK/${RULE_SPARE_ERROR_DINGTALK_WEBHOOK}/g" /opt/elastalert/rules/rule_spare_error.yaml
sed -i "s/RULE_SPARE_ERROR_DINGTALK_MSGTYPE/${RULE_SPARE_ERROR_DINGTALK_MSGTYPE}/g" /opt/elastalert/rules/rule_spare_error.yaml


sed -i "s/ELASTALERT_CONFIG_RUN_EVERY_SECONDS/${ELASTALERT_CONFIG_RUN_EVERY_SECONDS}/g" /opt/config/elastalert_config.yaml
sed -i "s/ELASTALERT_CONFIG_BUNFFER_TIME/${ELASTALERT_CONFIG_BUNFFER_TIME}/g" /opt/config/elastalert_config.yaml
sed -i "s/ELASTALERT_CONFIG_ES_HOST/${ELASTALERT_CONFIG_ES_HOST}/g" /opt/config/elastalert_config.yaml
sed -i "s/ELASTALERT_CONFIG_ES_PORT/${ELASTALERT_CONFIG_ES_PORT}/g" /opt/config/elastalert_config.yaml
sed -i "s/ELASTALERT_CONFIG_ES_USERNAME/${ELASTALERT_CONFIG_ES_USERNAME}/g" /opt/config/elastalert_config.yaml
sed -i "s/ELASTALERT_CONFIG_ES_PASSWORD/${ELASTALERT_CONFIG_ES_PASSWORD}/g" /opt/config/elastalert_config.yaml
sed -i "s/ELASTALERT_CONFIG_WRITEBACK_INDEX/${ELASTALERT_CONFIG_WRITEBACK_INDEX}/g" /opt/config/elastalert_config.yaml
sed -i "s/ELASTALERT_CONFIG_ALERT_TIME_LIMIT_DAYS/${ELASTALERT_CONFIG_ALERT_TIME_LIMIT_DAYS}/g" /opt/config/elastalert_config.yaml
sed -i "s/ELASTALERT_CONFIG_SMTP_HOST/${ELASTALERT_CONFIG_SMTP_HOST}/g" /opt/config/elastalert_config.yaml
sed -i "s/ELASTALERT_CONFIG_SMTP_PORT/${ELASTALERT_CONFIG_SMTP_PORT}/g" /opt/config/elastalert_config.yaml
sed -i "s/ELASTALERT_CONFIG_SMTP_SSL/${ELASTALERT_CONFIG_SMTP_SSL}/g" /opt/config/elastalert_config.yaml

sed -i "s/SMTP_USER/${SMTP_USER}/g" /opt/config/smtp_auth_file.yaml
sed -i "s/SMTP_PASSWORD/${SMTP_PASSWORD}/g" /opt/config/smtp_auth_file.yaml

echo "cat elastalert ......"
cat /opt/elastalert/elastalert_modules/dingtalk_alert.py
cat /opt/config/elastalert_config.yaml
cat /opt/config/smtp_auth_file.yaml
cat /opt/elastalert/rules/rule.yaml
cat /opt/elastalert/rules/rule_spare_error.yaml


echo "start elastalert ......"
sh  /opt/elastalert/run.sh
echo "start successfuly!!!"